#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
#
#  httpConnektion.py
#
#  Copyright 2011 Arne Hüffmeier <ahueffme@gmail.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#

import urllib.request
import urllib.parse
import re

from ObjektsF import Serie
from ObjektsF import Episode

apiKey = "12CE95C3AD7045F9"


def getSerie(name, language):
	print("Suche nach:", name)
	url = "http://thetvdb.com/api/GetSeries.php?seriesname=" + name \
	+ "&language=" + language
	url = url.replace(" ", "&#32;")
	try:
		httpSite = urllib.request.urlopen(url)
		content = httpSite.read()
		content = str(content, 'utf-8')
		httpSite.close()
		serien = re.findall(r"<Series>.*?</Series>", content, re.DOTALL)
		serienList = []
		for i in serien:
			name = re.findall(r"<SeriesName>.*</SeriesName>", i, re.S)
			name = name[0][12:-13]
			serienId = re.findall(r"<seriesid>.*</seriesid>", i, re.S)
			serienId = serienId[0][10:-11]
			over = re.findall(r"<Overview>.*</Overview>", i, re.S)
			try:
				over = over[0][10:-11]
			#print(over)
			except IndexError:
			#print ("jo bei", name)
				over = None
			first = re.findall(r"<FirstAired>.*</FirstAired>", i, re.S)
			if len(first) is not 0:
				first = first[0][12:-13]
			else:
				first = None
			temp = Serie(name, serienId, over, first)
			serienList.append(temp)
		return serienList
	except urllib.error.HTTPError:
		print("got a prblem with ther url", url)
	except urllib.error.URLError:
		print("got a prblem with ther url", url)


def getStaffeln(serienID, language):
	url = "http://thetvdb.com/api/" + apiKey + "/series/" + serienID \
	+ "/all/" + language + ".xml"
	httpSite = urllib.request.urlopen(url)
	content = httpSite.read()
	content = str(content, 'utf-8')
	httpSite.close()
	serienName = re.findall(r"<SeriesName>.*?</SeriesName>", content,
		re.S)[0][12:-13]
	episoden = re.findall("<Episode>.*?</Episode>", content, re.S)
	temp = [[], [], [], []]
	for i in episoden:
		name = re.findall("<EpisodeName>.*?</EpisodeName>", i, re.S)[0][13:-14]
		season = int(re.findall("<SeasonNumber>.*?</SeasonNumber>", i, re.S)[0]
		[14:-15])
		number = int(re.findall("<EpisodeNumber>.*?</EpisodeNumber>", i, re.S)[0]
		[15:-16])
		firstAired = re.findall("<FirstAired>.*?</FirstAired>", i, re.S)[0][12:-13]
		try:
			temp[season].append(Episode(serienName, season=season, epi=number,
			gName=name, gDate=firstAired))
		except IndexError:
			temp.append([Episode(serienName, season=season, epi=number,
			gName=name, gDate=firstAired)])
	return temp


if __name__ == "__main__":
	staffeln = getStaffeln("83462", "de")
	for i in staffeln:
		for j in i:
			j.printshort()